package com.lelefly.base.controller;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lelefly.base.model.FlyHistorySearchBean;
import com.lelefly.base.model.Result;
import com.lelefly.base.model.UploadInfoBean;
import com.lelefly.base.po.LeleFlyHistory;
import com.lelefly.base.po.LeleFlyInfo;
import com.lelefly.base.po.LeleflyHistoryInfo;
import com.lelefly.base.serviceImp.UploadFlyInfoService;
import com.lelefly.constant.Constant;

/**
 * 远程监控飞机飞行情况
 * @author KM
 *
 */
@Controller
@RequestMapping("/upload_plane")
public class UploadFlyInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(UploadFlyInfoController.class);
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
	@Resource
	private UploadFlyInfoService infoService;
	
	@RequestMapping(value="/upload_fly_info",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result uploadFlyInfo(@RequestBody String info){
		Result result = null;
		getLogger().info("更新飞行数据：" + sdf.format(new Date()));
		
		if (info == null) {
			result = new Result();
			result.setMes("数据转化错误");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		try {
			Gson gson = new Gson();
			Type type = new TypeToken<ArrayList<LeleFlyInfo>>(){}.getType();
			List<LeleFlyInfo> infoList = gson.fromJson(info, type);
			result = infoService.insertFlyInfo(infoList);
		} catch (Exception e) {
			e.printStackTrace();
			result = new Result<>();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("上传失败");
		}
		
		return result;
	}
	
	@RequestMapping(value="/upload_flying_info",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result uploadFlyingInfo(@RequestBody String info){
		Result result = new Result<>();
		if (StringUtils.isEmpty(info)) {
			result.setMes("上传数据为空");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		try {
			Gson gson = new Gson();
			Type type = new TypeToken<List<UploadInfoBean>>(){}.getType();
			List<UploadInfoBean> infoList = gson.fromJson(info, type);
			result = infoService.insertFlyingInfo(infoList);
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("上传失败");
		}
		return result;
	}
	
	@RequestMapping(value="/upload_fly_info_history",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result uploadFlyHistory(@RequestBody String info){
		Result result = null;
		getLogger().info("上传历史飞行记录：" + sdf.format(new Date()));
		
		if (StringUtils.isEmpty(info)) {
			result = new Result();
			result.setMes("数据为空");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		try {
			Gson gson = new Gson();
			Type type = new TypeToken<LeleFlyHistory>(){}.getType();
			LeleFlyHistory history = gson.fromJson(info, type);
			result = infoService.saveHistoryInfo(history);
		} catch (Exception e) {
			e.printStackTrace();result = new Result<>();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("JSON解析出错");
		}
		return result;
	}
	
	@RequestMapping(value="/search_fly_info",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result searchFlyHistory(@RequestBody String info){
		
		Result result = null;
		if (StringUtils.isEmpty(info)) {
			result = new Result();
			result.setMes("数据为空");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		try {
			Gson gson = new Gson();
			Type type = new TypeToken<FlyHistorySearchBean>(){}.getType();
			FlyHistorySearchBean history = gson.fromJson(info, type);
			result = infoService.searchFlyInfo(history);
		} catch (Exception e) {
			e.printStackTrace();result = new Result<>();
			result.setState(Constant.HTTP_STATUS_FAIL);
			result.setMes("JSON解析出错");
		}
		return result;
	}
	
	@RequestMapping(value="/search_fly_history",produces="application/json;charset=utf-8")
	@ResponseBody
	public Result<LeleflyHistoryInfo> queryHistoryInfo(FlyHistorySearchBean searchBean){
		
		Result<LeleflyHistoryInfo> result = new Result<>();
		
		if (searchBean == null) {
			result.setMes("查询数据为空");
			result.setState(Constant.HTTP_STATUS_FAIL);
			return result;
		}
		
		return result;
		
	}
}
